﻿using Microsoft.AspNetCore.Mvc;
using RuoVea.OmiApi.UserRoleMenu.Entitys;
using RuoVea.OmiApi.UserRoleMenu.Service;
using RuoVea.OmiApi.UserRoleMenu.Service.Dto;
using RuoVea.OmiUserRoleMenu.Attributes;

namespace RuoVea.OmiUserRoleMenu.Controllers;

/// <summary>
/// 菜单 
/// </summary> 
[AppRoute]
public class MenuController : Controller
{
    private readonly SysMenuService _sysMenuService;
    private readonly SysRoleMenuService _sysRoleMenuService;


    /// <summary>
    /// 
    /// </summary>
    /// <param name="sysMenuService"></param> 
    /// <param name="sysRoleMenuService"></param> 
    public MenuController(SysMenuService sysMenuService, SysRoleMenuService sysRoleMenuService)
    {
        _sysMenuService = sysMenuService;
        _sysRoleMenuService = sysRoleMenuService;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    [HttpGet(nameof(Index))]
    public IActionResult Index() { return View(); }
    /// <summary>
    /// 分页获取角色列表
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("pages")]
    public async Task<RestfulResult<PageResult<SysMenu>>> GetPagesAsync([FromBody] MenuInput data)
    {
        RestfulResult<PageResult<SysMenu>> restfulResult = new() { Code = CodeStatus.OK, Data = new PageResult<SysMenu>(1, 2000) };
        restfulResult.Data.Rows = await _sysMenuService.GetList(data);
        restfulResult.Data.TotalRows = 2000;
        return restfulResult;
    }
    /// <summary>
    /// 系统菜单列表（树表）
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("list")]
    public async Task<RestfulResult<List<SysMenu>>> GetMenuList([FromBody] MenuInput data)
    {
        RestfulResult<List<SysMenu>> restfulResult = new() { Code = CodeStatus.OK };
        restfulResult.Data = await _sysMenuService.GetList(data);
        return restfulResult;
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [UnitOfWork]
    [HttpPost("add")]
    public async Task<RestfulResult> AddAsync(AddMenuInput data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            await _sysMenuService.AddMenu(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }
    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("update")]
    public async Task<RestfulResult> UpdateAsync(UpdateMenuInput data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            await _sysMenuService.UpdateMenu(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }

    /// <summary>
    /// 删除系统菜单
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete("delete")]
    public async Task<RestfulResult> DeleteAsync(DeleteMenuInput data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            await _sysMenuService.DeleteMenu(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }

    /// <summary>
    /// 获取系统菜单树，用于新增、编辑时选择上级节点
    /// </summary>
    /// <returns></returns>
    [HttpGet("menutree")]
    public async Task<RestfulResult> GetMenuTree()
    {
        RestfulResult restful = new() { Code = CodeStatus.OK };
        restful.Data = await _sysMenuService.GetMenuTree();
        return restful;
    }

    /// <summary>
    /// 获取系统菜单树，用于给角色授权时选择
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("treeforgrant/{id:long}")]
    public async Task<RestfulResult> TreeForGrant(long id)
    {
        RestfulResult restful = new() { Code = CodeStatus.OK };
        if (id <= 0)
            return restful;

        restful.Data = await _sysMenuService.TreeForGrant(new TreeForGrantInput { RoleId = id });
        return restful;
    }

    /// <summary>
    /// 获取角色的菜单Id集合
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("rolemenuidlist/{id:long}")]
    public async Task<RestfulResult> GetRoleMenuIdList(long id)
    {
        RestfulResult restful = new() { Code = CodeStatus.OK };
        if (id <= 0)
            return restful;
        restful.Data = await _sysRoleMenuService.GetRoleMenuIdList(new List<long> { id });
        return restful;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("GrantMenu")]
    public async Task<RestfulResult> GrantMenu(RoleMenuInput data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };
        if (data.Id <= 0)
        {
            restful.Message = i18n.role_id_cannot_be_empty;
            return restful;
        }
        try
        {
            await _sysRoleMenuService.GrantRoleMenu(data);
            restful.Code = CodeStatus.OK;
            restful.Data = true;
            return restful;
        }
        catch (Exception ex)
        {
            restful.Code = CodeStatus.BadRequest;
            restful.Data = false;
            restful.Message = ex.Message;
            return restful;
        }
    }
}


